package thu.sorit;

import java.util.ArrayList;
import java.util.List;

import thu.sorit.net.Downloader;
import thu.sorit.resolver.Util;
import thu.sorit.resolver.Video;
import thu.sorit.resolver.VideoAdaptor;
import thu.sorit.service.SoritService;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.RelativeLayout;

public class Main extends Activity {

	private ServiceConnection connection = new ServiceConnection() {

        public void onServiceConnected(ComponentName name, IBinder binder) {
        	SoritService.SoritBinder b = (SoritService.SoritBinder) binder;
            service = b.getService();
    		fetchContent();
        }

        public void onServiceDisconnected(ComponentName name) {
            service = null;
        }

    };
	
    private SoritService service = null;
    
    private boolean isBound = false;
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		validateService();
	}
	
//	private List<Video> debuglist() {
//		Video v = new Video();
//		v.duration = 21;
//		v.expire = System.currentTimeMillis() + 100000;
//		v.preview = Bitmap.createBitmap(120, 90, Bitmap.Config.ARGB_8888);
//		v.rate = 100;
//		v.title = "Rylynn";
//		v.url = "http://www.tudou.com/programs/view/7EdK-n-gdiw/";
//		ArrayList<Video> arr = new ArrayList<Video>();
//		arr.add(v);
//		return arr;
//	}
	
	private void fetchContent() {
		if (service == null) {
			return;
		}
		ListView list = (ListView) this.findViewById(R.id.listView1);
		VideoAdaptor a = new VideoAdaptor(
				(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE),
				service.getVideoList());
		list.setAdapter(a);
		list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				if (service != null) {
					Intent i = new Intent(Intent.ACTION_VIEW);
					i.setData(Uri.parse(service.getVideoList().get(position).url));
					startActivity(i);
				}
			}

        });
	}

	@Override
    public void onDestroy() {
        super.onDestroy();
		if (isBound) {
			unbindService(connection);
			isBound = false;
		}
    }
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		menu.findItem(R.id.menu_start).setOnMenuItemClickListener(new OnMenuItemClickListener() {

			@Override
			public boolean onMenuItemClick(MenuItem item) {
				if (service != null) {
					service.startSort();
				}
				return true;
			}
			
		});
		return true;
	}
	
    private void validateService() {
        Intent intent = new Intent(this, SoritService.class);
        startService(intent);
        isBound = bindService(intent, connection, BIND_AUTO_CREATE);
    }

}
